package cn.iocoder.yudao.module.xhyy.controller.admin.hearing;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.xhyy.controller.admin.hearing.vo.*;
import cn.iocoder.yudao.module.xhyy.dal.dataobject.hearing.HearingDO;
import cn.iocoder.yudao.module.xhyy.service.hearing.HearingService;

@Tag(name = "管理后台 - 听力资源")
@RestController
@RequestMapping("/xhyy/hearing")
@Validated
public class HearingController {

    @Resource
    private HearingService hearingService;

    @PostMapping("/create")
    @Operation(summary = "创建听力资源")
    @PreAuthorize("@ss.hasPermission('xhyy:hearing:create')")
    public CommonResult<String> createHearing(@Valid @RequestBody HearingSaveReqVO createReqVO) {
        return success(hearingService.createHearing(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新听力资源")
    @PreAuthorize("@ss.hasPermission('xhyy:hearing:update')")
    public CommonResult<Boolean> updateHearing(@Valid @RequestBody HearingSaveReqVO updateReqVO) {
        hearingService.updateHearing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除听力资源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('xhyy:hearing:delete')")
    public CommonResult<Boolean> deleteHearing(@RequestParam("id") String id) {
        hearingService.deleteHearing(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得听力资源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('xhyy:hearing:query')")
    public CommonResult<HearingRespVO> getHearing(@RequestParam("id") String id) {
        HearingDO hearing = hearingService.getHearing(id);
        return success(BeanUtils.toBean(hearing, HearingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得听力资源分页")
    @PreAuthorize("@ss.hasPermission('xhyy:hearing:query')")
    public CommonResult<PageResult<HearingRespVO>> getHearingPage(@Valid HearingPageReqVO pageReqVO) {
        PageResult<HearingDO> pageResult = hearingService.getHearingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HearingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出听力资源 Excel")
    @PreAuthorize("@ss.hasPermission('xhyy:hearing:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHearingExcel(@Valid HearingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HearingDO> list = hearingService.getHearingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "听力资源.xls", "数据", HearingRespVO.class,
                        BeanUtils.toBean(list, HearingRespVO.class));
    }

}